package com.symbio.ishenapi.patient.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.common.controller.BaseController;
import com.symbio.ishenapi.patient.model.TmPatientMedicalRecords;
import com.symbio.ishenapi.patient.model.TmQuickPrescription;
import com.symbio.ishenapi.patient.model.base.BaseTmQuickPrescription;
import io.jboot.web.controller.annotation.RequestMapping;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

@RequestMapping("/quick")
public class QuickController extends BaseController {

    /*
   添加开药信息
   */
    public void saveQuick() {
        System.out.print("111111111111111111111");
        TmQuickPrescription tq = (TmQuickPrescription) getReqBodyObject(TmQuickPrescription.class);

        Date date = new Date();
        tq.setCreateTime(date);
        if (tq.getDoctorId() == null || "".equals(tq.getDoctorId())){
            renderJsonError(40003, "医生编号");
        }else if (tq.getUserId() == null || "".equals(tq.getUserId())){
            renderJsonError(40003,"患者编号");
        }else if (tq.getDrugDetails() == null || "".equals(tq.getDrugDetails())){
            renderJsonError(40003,"药品明细");
        }
        else {
            tq.save();
            renderJsonError(40001, "开药");
        }


    }

    /*
     * 查看开药信息
     * */
    public void findQuick(){
        TmQuickPrescription tq = (TmQuickPrescription) getReqBodyObject(TmQuickPrescription.class);
        List<Record> list=TmQuickPrescription.dao.findRr(tq.getId());
        if (list == null ) {
            renderJsonError(40003,"开药");
            return;
        }
        renderJsonRet(list);

    }

    /*
     * 修改开药信息
     * */

    public void postUpdateQuick(){           //修改
        TmQuickPrescription tp = (TmQuickPrescription) getReqBodyObject(TmQuickPrescription.class);
        TmQuickPrescription byId = TmQuickPrescription.dao.findById(tp.getUserId());

        if ( byId!=null){
            if (!StrKit.isBlank(tp.getUserId().toString())) byId.setUserId(tp.getUserId());
            if (!StrKit.isBlank(tp.getId().toString())) byId.setId(tp.getId());
            if (!StrKit.isBlank(tp.getDoctorId().toString())) byId.setDoctorId(tp.getDoctorId());
            if (!StrKit.isBlank(tp.getDrugDetails())) byId.setDrugDetails(tp.getDrugDetails());
            byId.setCreateTime(new Date());
            if (byId.update()){
                renderJsonRet(tp.getId());
            }else {
                renderJsonError(40005,"开药");
            }
        }else{
            renderJsonError(40003,"开药");
            return;
        }
    }

}